/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: UIButton.java 44 2008-05-14 18:22:13Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class UIButton extends UIElement
{
    public static final int STATE_NORMAL  = 0;
    public static final int STATE_DOWN    = 1;
    public static final int STATE_HOVER   = 2;
    
    private String _text = "";
    private String _tooltipText = null;
    
    private int _state;
    private boolean _mouseOver = false;
    
    private Image _mouseUpImage = null;
    private Image _mouseDownImage = null;
    private Image _mouseOverImage = null;
    
    private List _buttonListeners = null;

    
    public UIButton(int px, int py, int width, int height)
    {
        super(px, py, width, height);
        setState(STATE_NORMAL);
    }

    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        g.drawString(_text, screenX, screenY);
    }
    
    public void addButtonListener(
            UIButtonListener listener)
    {
        if (_buttonListeners == null) {
            _buttonListeners = new LinkedList();
        } else if (_buttonListeners.contains(listener)) {
            return;
        }
        _buttonListeners.add(listener);
    }
    
    private void notifyButtonListeners()
    {
        if (_buttonListeners == null) {
            return;
        }
        for (Iterator it = _buttonListeners.iterator(); it.hasNext(); )
        {
            UIButtonListener listener =
                (UIButtonListener)it.next();
            listener.buttonPressed(this);
        }
    }

    public void setMouseUpImage(Image mouseUpImage)
    {
        _mouseUpImage = mouseUpImage;
        setState(getState());
    }

    public void setMouseDownImage(Image mouseDownImage)
    {
        _mouseDownImage = mouseDownImage;
        setState(getState());
    }

    public void setMouseOverImage(Image mouseOverImage)
    {
        _mouseOverImage = mouseOverImage;
        setState(getState());
    }

    protected boolean mouseClicked(int button, int x, int y)
    {
        notifyButtonListeners();
        return false;
    }

    protected boolean mouseMoved(int newx, int newy)
    {
        setState(STATE_HOVER);
        _mouseOver = true;
        return false;
    }

    protected void mouseLeft(int newx, int newy)
    {
        setState(STATE_NORMAL);
        _mouseOver = false;
    }

    protected boolean mousePressed(int button, int x, int y)
    {
        setState(STATE_DOWN);
        return false;
    }

    protected boolean mouseReleased(int button, int x, int y)
    {
        setState(_mouseOver ? STATE_HOVER : STATE_NORMAL);
        return false;
    }

    protected void setState(int id)
    {
        _state = id;
        
        switch (_state)
        {
            case STATE_DOWN:
                setBackgroundImage(_mouseDownImage);
                break;
            case STATE_HOVER:
                setBackgroundImage(_mouseOverImage);
                break;
            default:
                setBackgroundImage(_mouseUpImage);    
        }

        
    }

    public String getText()
    {
        return _text;
    }

    public void setText(String text)
    {
        _text = text;
    }

    public int getState()
    {
        return _state;
    }

    public void setTooltipText(String tooltipText)
    {
        _tooltipText = tooltipText;
    }

    public String getTooltipText(int x, int y)
    {
        return _tooltipText != null ? _tooltipText :
            super.getTooltipText(x, y);
    }

}
